package br.com.wai2008.client.service;


/**
 * This class manages the Service on client. It works with one thread
 * per time.
 * 
 * @since 
 * 		September, 2008.
 * 
 * @version
 * 		WAI2008 - version 1.0
 * 
 * @author 
 * 		Andre Romero Goncales
 */
public class Service {

	/**
	 * Service Thread.
	 */
	private ServiceThread thread = null;

	/**
	 * Service's state. Could be STOPPED, RUNNING or CRASHED.
	 * 
	 * @see
	 * 		br.com.wai2008.client.service.ServiceState
	 */
	private int state = ServiceState.STATE_STOPPED;

	/**
	 * Start the service.
	 */
	public void startService() {
		System.out.println("[Service] Starting client...");
		this.thread = new ServiceThread();
		this.thread.start();
		System.out.println("[Service] Current Thread: " + this.thread.getId());
		this.state = ServiceState.STATE_RUNNING;
		System.out.println("[Service] Client Started");		
	}

	/**
	 * Stop the service.
	 */
	public void stopService() {
		System.out.println("[Service] Stopping client...");
		this.thread.stopRequest();		
		this.state = ServiceState.STATE_STOPPED;
		System.out.println("[Service] Client Stopped");
	}

	/**
	 * Get the Service's state.
	 * 
	 * @return
	 * 		0 - if state is STOPPED.<br>
	 * 		1 - if state is RUNNING.<br>
	 * 		2 - if state is CRASHED.
	 * 
	 * @see
	 * 		br.com.wai2008.client.service.ServiceState
	 */
	public int getServiceState() {
		return this.state;
	}

	/**
	 * Get the Service's state.
	 * 
	 * @return
	 * 		STOPPED, RUNNING, CRASHED or NOT FOUND.
	 * 
	 * @see
	 * 		br.com.wai2008.client.service.ServiceState
	 */
	public String getServiceStateName() {
		switch(this.state) {
		case (ServiceState.STATE_STOPPED):	
			return "STOPPED";
		case (ServiceState.STATE_RUNNING):	
			return "RUNNING";
		case (ServiceState.STATE_CRASHED):	
			return "CRASHED";
		default:
			return "NOT FOUND";
		}
	}
	
}
